package com.badibadi.uniclubber;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.badibadi.adapter.GridViewListAdapter;
import com.badibadi.infos.MailXiangXiModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.HtmlUtils;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.RegexUtils;
import com.badibadi.mytools.Utils;
import com.lidroid.xutils.util.LogUtils;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

public class AcceptHuiYuanMailActivity extends AcceptClubMailActivity {

	// private String
	private String jijian_UserName;// 寄件的用户
	private String Accept_ClubName;// 收件的俱乐部名称
	String zhanneixinId;// 站内信的id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		zhanneixinId = getIntent().getStringExtra("zhanneixinId");
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_accept_hui_yuan_mail);
		jijian_UserName = getIntent().getStringExtra("jijian_UserName");
		Accept_ClubName = getIntent().getStringExtra("Accept_ClubName");

		((TextView) (this.findViewById(R.id.jijianfang))).setText(""
				+ jijian_UserName);
		((TextView) (this.findViewById(R.id.shoujianfang))).setText(""
				+ Accept_ClubName);

		this.findViewById(R.id.delete).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						delete(Utils.getUid(AcceptHuiYuanMailActivity.this),
								zhanneixinId, "zhannei_contactclub", "",
								handler);
					}
				});

	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

		TextView content = (TextView) this.findViewById(R.id.content);
		// content.setText();
		GridView item_gridView = (GridView) this.findViewById(R.id.item);
		// 获取所有的包含图片的<src= >类似的html语言
		List<String> list = null;
		if (mailXiangXiModel.getContents() != null) {
			list = RegexUtils.getImgSrc(mailXiangXiModel.getContents());
//			for (int i = 0; i < list.size(); i++) {
//				LogUtils.d("列表是：==》" + list.get(i).toString());
//			}

		}
		if (list != null && !list.isEmpty()) {
//			LogUtils.d("列表是：==》不是空");
			GridViewListAdapter adapter = new GridViewListAdapter(
					AcceptHuiYuanMailActivity.this, list);
			item_gridView.setAdapter(adapter);
			String aa = mailXiangXiModel.getContents();

			List<String> listgroup = RegexUtils.getImgSrcGroup(aa);
			for (int i = 0; i < listgroup.size(); i++) {

				// <img src=' ' />
				if(listgroup.get(i).indexOf(Constants.Fei_biaoqing_qianzui)!=-1 || list.get(i).indexOf(Constants.Fei_biaoqing_qianzui2)!=-1 || list.get(i).indexOf(Constants.Fei_biaoqing_qianzui3)!=-1 || list.get(i).indexOf(Constants.Fei_biaoqing_qianzui4)!=-1)
				aa = aa.replaceAll(listgroup.get(i), "");// ("<img src='"+list.get(i)+"' />","");

			}

			new HtmlUtils(AcceptHuiYuanMailActivity.this, content, aa)
					.showTextView();
//			LogUtils.d("aa=" + aa);
			// <img src=' '/ >

		} else if (list == null || list.isEmpty()) {
//			LogUtils.d("列表是：==》是空");
			new HtmlUtils(AcceptHuiYuanMailActivity.this, content,
					mailXiangXiModel.getContents()).showTextView();
		}
		// new HtmlUtils(AcceptHuiYuanMailActivity.this,content,
		// mailXiangXiModel.getContents(),180).showTextView();

	}

	@Override
	protected void showClubNotice(String uid, String zhanneiId) {
		showContactClub(Utils.getUid(this), zhanneixinId);
	};

	/**
	 * 这是会员联系俱乐部
	 * 
	 * @param uid
	 * @param zhanneiId
	 */
	public void showContactClub(final String uid, final String zhanneiId) {
		Utils.showPrgress(AcceptHuiYuanMailActivity.this);

		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
				map.put("zhanneiId", zhanneiId);

				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "inform/showContactClub");
				if (content != null) {
					//    ("返回的投票数据是:" + content);
					Results results3 = Utils.checkResult_NNN(
							AcceptHuiYuanMailActivity.this, content);
					if (results3 != null
							&& !results3.getRetmsg().equals("null")) {

						try {
							mailXiangXiModel = JSONUtils.getEntityByJsonString(
									results3.getRetmsg(),
									MailXiangXiModel.class);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}

						handler.sendEmptyMessage(2);
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

}
